/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.controller.manage.rank;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.BaseController;
import shop.core.common.bean.JSONResult;
import shop.services.manage.rank.RankService;
import shop.services.manage.rank.bean.Rank;

import java.math.BigDecimal;

/**
 * @类名称：RankAction
 * @创建人：Ltz
 * @创建时间：2017-10-13 下午16:10:42      
 * @版本号：1.0
 * @描述：
 */
@Controller
@RequestMapping("/manage/rank/")
public class RankAction extends BaseController<Rank> {
    private static final Logger logger = LoggerFactory.getLogger(RankAction.class);
    @Autowired
    private RankService rankService;
    private static final String page_toList = "/manage/rank/rankList";
    public RankService getService() {
        return rankService;
    }

    private RankAction(){
        super.page_toList = page_toList;
        super.page_toAdd = null;
        super.page_toEdit = null;
    }


    /**
     * 异步编辑单个对象--获取会员等级信息的时候百分比要乘以100
     * @param e
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("toEditRankJson")
    @ResponseBody
    public JSONResult toEditRankJson(@ModelAttribute("e") Rank e, ModelMap model) throws Exception {
        e = getService().selectOne(e);
        BigDecimal b1 = new BigDecimal(100);
        e.setPercent(e.getPercent().multiply(b1));
        JSONResult jsonResult = new JSONResult();
        jsonResult.setData(e);
        return jsonResult;
    }

    /**
     * 获取会员等级信息--更改百分比状态
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "updateRankJson", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult updateRankJson(@ModelAttribute("e") Rank e) throws Exception {
        JSONResult jsonResult = new JSONResult();
        BigDecimal percent = new BigDecimal(100);                        //设置被除数
        BigDecimal setScale = e.getPercent().setScale(4, BigDecimal.ROUND_HALF_UP);        //保留四位小数
        BigDecimal percent2 = setScale.divide(percent);                 //相除获取百分比
        e.setPercent(percent2);                                         //设置抽成百分比
        getService().update(e);
        return jsonResult;
    }
}
